<template>
  <view class="main">
    <view class="inputCard">
      <view class="inputBox" style="border-bottom: 1px solid #f5f5f5">
        <view class="title">服务仓</view>
        <up-input placeholder="" border="none" v-model="formData.information"></up-input>
      </view>
      <view class="inputBox">
        <view class="title">商品名称</view>
        <up-input placeholder="请输入商品名称" border="none" v-model="formData.name"></up-input>
      </view>
    </view>

    <view class="textBox">
      <view>其他描述</view>
      <u-textarea maxlength="50" border="none" v-model="formData.content" placeholder="请详细描述你的问题" :count="true"></u-textarea>
    </view>
    <view class="scBox">
      <image v-if="formData.image" class="box" :src="formData.image" mode=""></image>
      <view v-else class="box" @click="uploadImg">
        <u-icon name="plus" color="#C9CDD8" size="30"></u-icon>
      </view>
    </view>
    <view class="bottomBox">
      <u-button @click="submit" text="提交信息" shape="circle" color="linear-gradient(90deg, #69E07D 0%, #2ED19A 33%, #1BBD7E 100%)"></u-button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { BASE_UPLOADURL } from "../../utils/request";
const formData = ref({
  information: "浙江明辉蔬果",
  name: "",
  content: "",
  image: "",
});
const uploadImg = () => {
  uni.chooseImage({
    count: 1, //默认9
    sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
    sourceType: ["album", "camera"], //从相册选择或者打开相机
    success: function (res) {
      let tempFilePaths = res.tempFilePaths;
      for (let i = 0, len = tempFilePaths.length; i < len; i++) {
        uni.uploadFile({
          url: BASE_UPLOADURL, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[i],
          name: "file",
          header: {
            Authorization: uni.getStorageSync("token"),
          },
          success: (uploadFileRes) => {
            formData.value.image = JSON.parse(uploadFileRes.data).url;
          },
        });
      }
    },
  });
};

const submit = async () => {
  uni.showToast({
    title: "提交成功",
  });
  setTimeout(() => {
    uni.switchTab({
      url: "/pages/home/home",
    });
  }, 1000);
};
</script>

<style lang="less" scoped>
.textBox {
  margin-top: 20rpx;
  background: #fff;
  padding: 32rpx;
  border-radius: 20rpx;
  .u-textarea {
    padding: 0;
    margin-top: 24rpx;
  }
}
.scBox {
  display: flex;
  background: #fff;
  margin-top: 20rpx;
  padding: 32rpx;
  border-radius: 20rpx;

  .box {
    width: 160rpx;
    height: 160rpx;
    border-radius: 8rpx;
    background: #f5f8fc;
    margin-right: 5rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }
}
.inputCard {
  background: #ffffff;
  margin-top: 20rpx;
  padding: 32rpx;
  border-radius: 20rpx;
  .inputBox {
    display: flex;
    align-items: center;
    border-radius: 20rpx;
    padding-bottom: 34rpx;
    .title {
      width: 180rpx;
      font-size: 28rpx;
      color: #1e1e1e;
      line-height: 48rpx;
    }
    &:last-child {
      padding-bottom: 0rpx;
      padding-top: 24rpx;
    }
  }
}
</style>
